<template>
  <div class='container'>
    <el-dialog :title="title" :visible.sync="dialogVisible">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody" class="content">
          <quill-editor
            v-model="ruleForm.articleBody"
            :options="editorOption">
          </quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" prop="videoURL">
          <el-input v-model="ruleForm.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="Hidden">取 消</el-button>
        <el-button type="primary" :loading="isloading" @click="submitForm('ruleForm',ruleForm)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles.js'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  components: {
    quillEditor
  },
  props: {
    // 弹框标题
    title: {
      type: String,
      required: true
    },
    edit: {
      type: Object,
      required: true
    },
    getList: {
      type: Function,
      required: true
    }
  },
  data () {
    return {
      // loading
      isloading: false,
      // 对富文本编辑器的配置
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            // [{ header: 1 }, { header: 2 }, { header: 3 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            // [{ indent: '-1' }, { indent: '+1' }],
            ['image'],
            ['link']
          ]
        }
      },
      dialogVisible: false,
      // 因为编辑文章会传入 edit对象，所以不需要再定义ruleForm
      ruleForm: {
        title: this.edit.title,
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请选择活动区域', trigger: 'blur' }
        ],
        videoURL: [
          { pattern: /^https?:\/\/(.+\/)+.+(\.(swf|avi|flv|mpg|rm|mov|wav|asf|3gp|mkv|rmvb|mp4|m4v))$/, message: '视频播放地址格式不正确,您可选择不填哟~' }
        ]
      }
    }
  },
  // 如果是在data里面创建后，给ruleform赋初始值的话，会没有效果，因为data创建时，edit中还没有值，所以需要用watch监听edit的数据，监听以后，点击修改时，给edit重新赋值，此时edit变化了
  watch: {
    edit: function () {
      this.ruleForm.title = this.edit.title
      this.ruleForm.articleBody = this.edit.articleBody
      this.ruleForm.videoURL = this.edit.videoURL
    },
    deep: true
  },
  methods: {
    // 弹层显示
    Visble () {
      this.dialogVisible = true
    },
    Hidden () {
      this.dialogVisible = false
    },
    // 确定发布
    submitForm (formInfo, ruleForm) {
      // loading
      this.isloading = true
      // 整体校验
      this.$refs[formInfo].validate(async (valid) => {
        if (valid) {
          // this.hEdit(formInfo, edit)
          // this.articleAdd()
          if (this.edit.id) {
            // 2.提交修改
            console.log('我是修改')
            this.hEdit(formInfo, this.ruleForm)
            this.$message.success('修改文章成功！')
          } else {
            console.log('我是添加')
            this.articleAdd()
            this.$message.success('新增文章成功！')
          }
          // 1.更新列表--2.关闭弹窗
          // 1.更新列表--重新渲染父组件页面
          this.getList()
          // 2.关闭弹框
          this.Hidden()
          // 3.loading
          this.isloading = false
        } else {
          console.log('error submit!!')
          this.$message.error('提交错误，请重试！！！')
          this.isloading = false
          return false
        }
      })
    },
    // 发请求-添加文章
    async articleAdd () {
      const res = await add(this.ruleForm)
      console.log('发请求：', res)
    },
    // 发请求-修改文章
    async hEdit (formIndo, ruleForm) {
      console.log('修改的数据信息：', ruleForm)
      const res = await update({ id: this.edit.id, title: ruleForm.title, articleBody: ruleForm.articleBody, videoURL: ruleForm.videoURL })
      console.log(res)
    }
  }
}
</script>

<style scoped>
.quill-editor{
  height: 280px;
}
.content {
  height: 340px;
}
</style>
